package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.annotation.DeleteAnnotation;
import com.baizhi.annotation.InsertAnnotation;
import com.baizhi.dao.GuruDao;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import lombok.Data;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class GuruServiceImpl implements GuruService {
    @Autowired(required = false)
    private GuruDao guruDao;

    @Override
    @InsertAnnotation
    public Map selectAll(int page, int limit, String guruName) {
        int offset=(page-1)*limit;
        List<Guru> list=guruDao.selectAll(offset,limit);
        int count=guruDao.selectCount(guruName);

        Map map=new HashMap();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");


        return map;
    }

    @Override
    @DeleteAnnotation
    public Map insert(Guru guru) {
        int count=guruDao.insert(guru);
        Map map=new HashMap();
        if(count>0){
            map.put("isInsert",true);
        }else{
            map.put("isInsert",false);
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map delete(int id) {
        int count=guruDao.delete(id);
        Map map=new HashMap();
        if(count>0){
            map.put("isDelete","删除成功");
        }else{
            map.put("isDelete","删除失败");
        }

        return null;
    }

    @Override
    @DeleteAnnotation
    public Map update(Guru guru) {
        int count=guruDao.update(guru);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","更新成功");
        }else{
            map.put("isUpdate","更新失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map updatea(Guru guru) {
        int count=guruDao.updatea(guru);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","更新成功");
        }else{
            map.put("isUpdate","更新失败");
        }
        return null;
    }

    @Override
    @InsertAnnotation
    public Guru selectOne(int id) {

        return guruDao.selectOne(id);
    }


    @DeleteAnnotation
    public Map updates(Guru guru){

        if(guru.getGuruStatus()==2){
            guru.setGuruStatus(1);
        }else{
            guru.setGuruStatus(2);
        }
        int count=guruDao.updates(guru);
        Map map=new HashMap();
        System.out.println(guru.getGuruId()+"guruIdService");
        System.out.println(guru.getGuruStatus()+"guruStatusService");
        System.out.println(guru);
        if(count>0){
            map.put("isUpdates","修改成功");
        }else{
            map.put("isUpdates","修改失败");
        }
        return map;
    }

    @Override
    @InsertAnnotation
    public Map selectAlls() {
        List list = guruDao.selectAlls();
        Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams("上师信息表","guru"),Guru.class,list);
        try {
            workbook.write(new FileOutputStream("C://Users//小龙//Desktop//新建文件夹//guru.xlsx"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map=new HashMap();
        map.put("data",list);
        if(workbook!=null){
            map.put("isxiazai","导出成功");
        }else{
            map.put("isxiazai","导出失败");
        }
        return map;
    }
}
